package cn.com.bsoft.mobile.ui

import cn.com.bsoft.mobile.model.App
import cn.com.bsoft.mobile.model.DeviceType
import cn.com.bsoft.mobile.service.AppleApnsService
import cn.com.bsoft.mobile.service.MobileAppService
import cn.com.bsoft.mobile.service.toRsp
import cn.net.xyan.blossom.core.service.ConfigService
import cn.net.xyan.blossom.webUI.ui.textAreaWithUpload
import cn.net.xyan.blossom.webUI.utils.getBean
import com.github.vok.karibudsl.*
import com.vaadin.data.Converter
import com.vaadin.data.Result
import com.vaadin.data.ValueContext
import com.vaadin.server.Page
import com.vaadin.shared.Position
import com.vaadin.shared.ui.MarginInfo
import com.vaadin.ui.*
import com.vaadin.ui.themes.ValoTheme
import java.util.HashMap

fun FormLayout.section(caption: String, style: String = ValoTheme.LABEL_H2, block: Label.()->Unit={}) = init(Label(caption)) {
    addStyleNames(style, ValoTheme.LABEL_COLORED)
    block()
}

class AppSuiteList: VerticalLayout() {

    var app:App? = null

    init {
        isSpacing = false
        margin = MarginInfo(false, false, false, false)
        val self = this
        setSizeFull()

        val appService = getBean(MobileAppService::class.java)!!

        val configService = getBean(ConfigService::class.java)!!

        val binder = beanValidationBinder<App>()

        horizontalLayout {
            //alignment = Alignment.BOTTOM_LEFT
            val cbApp = comboBox<String>(caption = "应用") {

                val map = HashMap<String, String?>()

                appService.allApp().forEach {
                    map[it.nativeID] = it.title
                }

                setItems({ caption: String, text: String -> caption.contains(text, ignoreCase = true) },
                        map.keys)

                setItemCaptionGenerator {
                    map[it]
                }

                isEmptySelectionAllowed = false
                isTextInputAllowed = false

                addValueChangeListener {
                    val appID = it.value
                    app = appService.queryApp(appID)?.toRsp()

                    binder.readBean(app)
                }


            }
            button("QRCode"){
                alignment = Alignment.BOTTOM_LEFT
                onLeftClick {
                    val appID = cbApp.value
                    val appRoot = configService.queryValue(ConfigService.KEYAppRoot)
                    val appURL = "$appRoot/app/inHouse/$appID"

                    val c = QRCodeComponent(appURL)

                    c.show()
                }
            }
            button("保存") {
                alignment = Alignment.BOTTOM_LEFT
                onLeftClick {
                    val appID = cbApp.value
                    if (app != null) {
                        binder.writeBean(app)

                        appService.saveApp(app!!)
                        Notification("成功", "保存成功", Notification.Type.ASSISTIVE_NOTIFICATION).apply {
                            styleName += " " + ValoTheme.NOTIFICATION_CLOSABLE
                            position = Position.TOP_CENTER
                            show(Page.getCurrent())
                        }



                        if( DeviceType.iOS == app!!.type){
                            val apnsService = getBean(AppleApnsService::class.java)
                            val mapp = appService.queryApp(app!!.appID)
                            apnsService?.updateClient(mapp!!)
                        }
                    }
                }
            }
        }

        formLayout {
            expandRatio = 1.0f
            setSizeFull()
            isMargin = false

            section("应用信息:")

            textField ("AppID"){
                w = 50.perc
                isReadOnly = true
                bind(binder).trimmingConverter().bind(App::appID.name)
            }

            textField ("套件"){
                w = 50.perc
                //isReadOnly = true
                bind(binder).trimmingConverter().bind(App::suiteID.name)
            }

            textField ("Type"){
                w = 50.perc
                isReadOnly = true
                bind(binder).withConverter(object : Converter<String?, DeviceType?> {
                    override fun convertToModel(value: String?, context: ValueContext?): Result<DeviceType?> {
                        return Result.ok(DeviceType.fromString(value?:""))
                    }

                    override fun convertToPresentation(value: DeviceType?, context: ValueContext?): String? {
                        return value?.value
                    }

                }).bind(App::type.name)
            }

            textField ("名称"){
                w = 50.perc
                bind(binder).trimmingConverter().bind(App::title.name)
            }

            section("推送信息:")

            textField ("Host"){
                w = 50.perc
                bind(binder).trimmingConverter().bind(App::pushHost.name)
            }

            textField ("TeamID"){
                w = 50.perc
                bind(binder).trimmingConverter().bind(App::teamID.name)
            }

            textField ("KeyID"){
                w = 50.perc
                bind(binder).trimmingConverter().bind(App::pushKeyID.name)
            }

//            textArea  ("Secret"){
//                setSizeFull()
//                expandRatio = 1.0f
//                bind(binder).trimmingConverter().bind(App::pushSecret.name)
//            }

            textAreaWithUpload("Secret"){
                setSizeFull()
                expandRatio = 1.0f
                bind(binder).trimmingConverter().bind(App::pushSecret.name)
            }

//            val fieldSecret = TextAreaWithUpload()
//
//            fieldSecret.bind(binder).trimmingConverter().bind(App::pushSecret.name)
//
//            fieldSecret.caption = "Secret"
//
//            addComponent(fieldSecret)

        }

    }
}